package cn.cnsuh.剑指offer.q53_在排序数组中查找数字;

public class Main {
    public static void main(String[] args) {
        int[] arr = {5,7,7,8,8,10};
        int target = 8;
        System.out.println("search(arr) = " + search(arr,target));
    }
    static int search(int[] nums, int target) {
        if(nums==null||nums.length==0) return 0;

        int l = 0,r = nums.length-1;

        while (l < r){
            int mid = l + r >> 1;
            if(nums[mid] >= target){
                r = mid;
            }else{
                l = mid + 1;
            }
        }
        int left = l;
        if(nums[r] == target){
            r = nums.length-1;
            while (l < r){
                int mid = l+r+1>>1;
                if(nums[mid] <= target){
                    l = mid;
                }else{
                    r = mid - 1;
                }
            }
        }else{
            return 0;
        }
        int right = r;
        return right-left+1;
    }
}
